<?php

namespace App\Admin\Controllers;

use App\Admin\Models\Users;
use Illuminate\Http\Request;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Show;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use function foo\func;

use App\Admin\Extensions\Check;
class UsersController extends AdminController
{
    protected $title = '用户管理';

    protected function grid()
    {
        $grid = new Grid(new Users);
        $grid->disableCreateButton();
        $grid->disableExport();
        $auth = Auth::guard('admin')->user()->toArray();
        $uid = Users::whereAccount($auth['account'])->value('id');
        $level = Users::whereAccount($auth['account'])->value('level');
        if($auth['account']){
            if($level == 1){
                $array = array();
                $array = DB::table('user_relation')->where('pid',$uid)->where('lay','<=','2')->pluck('user_id')->toArray();
                $grid->model()->whereIn('id',$array)->where('level','0');
            }
        }else{
            $grid->model()->where('level','0');
        }
        if($level == 1){
            $grid->disableActions();
        }
        $grid->filter(function($filter){
            $filter->disableIdFilter();

            $filter->column(1/3, function ($filter) {
                $filter->equal('account', 'UID');
            });
            $filter->column(1/3, function ($filter) {
                $filter->like('nickname','微信昵称');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('Parent.p_account','上级UID');
            });
            $filter->column(1/3, function ($filter) {
                $filter->equal('is_auth','认证状态')->select([
                    1    => '未认证',
                    2    => '认证中',
                    3    => '拒绝',
                    4    => '已认证'
                ]);
            });
        });
        $grid->model()->orderBy('id','desc');
        $grid->number('序号');
        $grid->rows(function ($row, $number) {
            $page = request('page');
            if($page){
                $page = ($page-1)*10;
            }
            $row->column('number',$page + $number+1);
        });
        $grid->column('account','用户UID');
        $grid->column('watchid','微信Openid');
        $grid->column('avatar', '头像')->lightbox(['width' => 40, 'height' => 40]);
//        $grid->challenge_map()->lightbox(['width' => 50, 'height' => 50]);
        $grid->column('nickname','微信昵称');
//        $grid->column('realname','真实姓名');
        $grid->column('parentid','上级UID')->display(function (){
            $id = $this->id;
            $ids = DB::table('user_relation')->where('user_id',$id)->where('lay','1')->value('pid');
            return DB::table('users')->whereId($ids)->value('account');
        });
        $grid->column('parent','上级昵称')->display(function (){
            $id = $this->id;
            $ids = DB::table('user_relation')->where('user_id',$id)->where('lay','1')->value('pid');
            return DB::table('users')->whereId($ids)->value('nickname');
        });
        $grid->column('agent','代理账号')->display(function (){
            $id = $this->id;
            $ids = DB::table('user_relation')->where('user_id',$id)->where('lay','2')->value('pid');
            return DB::table('users')->whereId($ids)->value('admin_name');
        });
        $grid->column('level','用户身份')->display(function ($level){
            switch ($level){
                case 0:
                    return "普通用户";
                    break;
                case 1:
                    return "代理商";
                    break;
            }
        });
        $grid->column('is_auth','是否认证')->using([
            1 => '未认证',
            2 => '认证中',
            3 => '拒绝',
            4 => '已认证',
        ])->dot([
            1 => 'primary',
            2 => 'info',
            3 => 'danger',
            4 => 'success'
        ]);
        $grid->column('note','认证拒绝原因');
        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
            // 去掉编辑
//            $actions->disableEdit();
            // 去掉查看
            $actions->disableView();
            if($actions->row->status == 2){
                $actions->append(new Agree);
                $actions->append(new Refuse);
            }
        });

        $grid->disableActions(false);
        $grid->actions(function ($actions) {
            $actions->disableDelete();
            $url = '/zec/Users/check';
            if($actions->row->is_auth == Users::USER_STATUS_DEFAULT){
                $actions->append(new Check(0, $actions->getKey(), $url, '', '拒绝'));
                $actions->append(new Check(1, $actions->getKey(), $url, '', '通过'));
            }elseif($actions->row->is_auth == 1){
                $actions->append('未提交审核');
            }else{
                $actions->append('已审核');
            }
        });
        return $grid;
    }
    protected function detail($id){
        $show = new Show(Users::findOrFail($id));
        $show->field('realname','真实姓名');
        $show->field('card','身份证号');
        $show->card_img('身份证正面照')->image('card_img', '身份证正面照');
        $show->card_imgs('身份证正面照')->image('card_imgs', '身份证反面照');
        $show->image('免冠照')->image('image', '免冠照');
        return $show;
    }
    protected function form(){
        $form = new Form(new Users);
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });
        $form->saving(function (Form $form) {
            $userid = DB::table('users')->whereAccount($form->account)->value('id');
            if(DB::table('user_relation')->where('user_id',$userid)->first()){
                return back()->with(admin_toastr('已存在上级', 'error'));
            }
            if(DB::table('users')->where('admin_name',$form->admin_name)->first()){
                return back()->with(admin_toastr('该后端账号已存在', 'error'));
            }
            if($form->level == 0){
                return back()->with(admin_toastr('身份未发生改变，请确认', 'error'));
            }
        });
        $form->text('nickname','昵称')->disable();
        $form->mobile('mobile','电话');
        $form->text('admin_name','分配后端账号');
        $form->text('admin_pass','分配后端密码');
        $form->radio('level','用户身份')->options(['0'=>'普通用户','1'=>'代理商'])->default('0');
        return $form;
    }

    public function check(Request $request){
        $id = $request->post('id');
        $status = $request->post('stat');
        $note = $request->post('reason');
        if($status != Users::USER_STATUS_REFUSE && $status != Users::USER_STATUS_SUCCESS){
            return ['code'=>'error','msg'=>'参数无效'];
        }
        $uinfo = Users::find($id);
        if($uinfo->is_auth != 2){
            return ['code' => 'error','msg' => '异常操作'];
        }

        $uinfo->is_auth = $status;
        $uinfo->note = $note;
        $uinfo->updated_at = date('Y-m-d H:i:s',time());
        if($uinfo->save()){
            return ['code' => 'success','msg' => '审核成功'];
        }else{
            return ['code' => 'error','msg' => '审核失败'];
        }
    }
}
